define(['jquery','template','templates'], function ($,template,temp) {
        function setParam (name, value) {
            localStorage.setItem(name, value)
        }
        function getParam(name) {
            return localStorage.getItem(name)
        }
        function Encode(str) {
            var s = "";
            if (str.length == 0) return "";
            s = str.replace(/&/g, "&amp;");
            s = s.replace(/</g, "&lt;");
            s = s.replace(/>/g, "&gt;");
            s = s.replace(/ /g, "&nbsp;");
            s = s.replace(/\'/g, "&#39;");
            s = s.replace(/\"/g, "&quot;");
            return s;
        }
        function Decode(str) {
            var s = "";
            if (str.length == 0) return "";
            s = str.replace(/&amp;/g, "&");
            s = s.replace(/&lt;/g, "<");
            s = s.replace(/&gt;/g, ">");
            s = s.replace(/&nbsp;/g, " ");
            s = s.replace(/&#39;/g, "\'");
            s = s.replace(/&quot;/g, "\"");
            return s;
        }
        function renderTemplate(data) {
            try
            {
                var pageHtml = "templates." + data.templateId;
                $("#" + data.containerId).html(eval(pageHtml));
                var renderHtml = template(data.templateId,data);
                $("#" + data.containerId).html(renderHtml);
            }
            catch (e)
            {
                console.log("模版渲染失败");
                console.log(e);
            }
        }
    function Demodata(){
        Mock.mock('http://192.168.1.132:1315/api/apps', {'result|5':[{
            'Id'	   : '@id',
            'Name'	   : '@name',
            'Url'	   : '@url',
            'Img'	   : '@img'
        }]});
    }

    function _ajax(Type,Url,containerId,templateId) {
      //  Demodata();
        $.ajax({
            dataType: 'text',
            async: true,
            cache: false,
            type: Type,
            url: Url,
            beforeSend: function (XMLHttpRequest) {
                //console.log(XMLHttpRequest);
            },
            success: function (result) {
                var D = JSON.parse(result);
                if (typeof containerId == "string" && typeof templateId == "string")
                {
                    renderTemplate({
                        containerId:containerId,
                        templateId:templateId,
                        data:D
                    });
                }
                else
                {
                    console.log("无效的模版!");
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                //console.log(XMLHttpRequest, textStatus, errorThrown);
            },
            complete: function (XMLHttpRequest, textStatus) {
                //console.log(XMLHttpRequest, textStatus);
                switch (containerId)
                {
                    case 'topmenu-container':
                        StyleClick(".topmenu");
                        break;
                    case 'leftmenu-container':
                        StyleClick(".leftmenu");
                        break;
                    case 'content-container':
                        StyleClick(".content");
                        break;
                }
            }
        })
    }

    function StyleClick(cssname){
        var appli = $(cssname).find("a");
        $(appli).click(function(){
            for (i=0;i<appli.length;i++)
            {
                $(appli[i]).css("background-color","white");
            }
            $this = $(this);
            $($this).css("background-color","#cccccc");
        })
    }

        return {
            setParam: setParam,
            getParam: getParam,
            Encode: Encode,
            Decode: Decode,
            renderTemplate:renderTemplate,
            _ajax: _ajax,
            StyleClick:StyleClick
        }

    if (typeof require != 'undefined') {
        define(['jquery'], function ($) {
            return utils($);
        })
    } else {
        window.utils = utils($);
    }
});